﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Documents;
using System.IO;
using System.Windows;

namespace CodeSnippetsWPF.Model
{
    public class CodeSnippet : INotifyPropertyChanged
    {
        /// <summary>
        /// Property changed Event handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private CodeSnippetsDB _DBM;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        private string _CODE_LANGUAGE;
        private string _CODE_TITLE;
        private string _CODE_SNIPPET;
        private string _CODE_RAW;
        private int _CODE_ID;
        public CodeSnippet(int id, string title, string language, string codesnippet,string coderaw, CodeSnippetsDB DBM)
        {
            _CODE_ID = id;
            _CODE_TITLE = title;
           _CODE_LANGUAGE = language.ToUpper();
            _CODE_SNIPPET = codesnippet;
            _CODE_RAW = coderaw;
            _DBM = DBM;
            
        }


        public string CODE_RAW
        {
            get { return _CODE_RAW; }
            set
            {
                _CODE_RAW = value;
                OnPropertyChanged(new PropertyChangedEventArgs("CODE_RAW"));
                _DBM.UpdateRow(_CODE_ID,"CODE_RAW",value);
            }

        }
        public int CODE_ID
        {
            get { return _CODE_ID; }
            set
            {
                _CODE_ID = value;
                OnPropertyChanged(new PropertyChangedEventArgs("CODE_ID"));
            }
        }
        public string CODE_LANGUAGE
        {
            get { return _CODE_LANGUAGE; }
            set
            {
                _CODE_LANGUAGE = value;
                OnPropertyChanged(new PropertyChangedEventArgs("CODE_LANGUAGE"));
                _DBM.UpdateRow(_CODE_ID, "CODE_LANGUAGE", value);
            }
        }
        public string CODE_TITLE
        {
            get { return _CODE_TITLE; }
            set
            {
                _CODE_TITLE = value;
                OnPropertyChanged(new PropertyChangedEventArgs("CODE_TITLE"));
                _DBM.UpdateRow(_CODE_ID, "CODE_TITLE", value);

            }
        }
        public string CODE_SNIPPET
        {
            get { return _CODE_SNIPPET; }
            set
            {
                _CODE_SNIPPET = value;


                if (_CODE_SNIPPET.Contains("<") && _CODE_SNIPPET.Contains(">"))
                {
                string valuestring = _CODE_SNIPPET;
                var flowDocument = new FlowDocument();
                var text = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd); 
                text.Load(new MemoryStream(Encoding.UTF8.GetBytes(valuestring)), DataFormats.Xaml);
                CODE_RAW = text.Text;
               }







                OnPropertyChanged(new PropertyChangedEventArgs("CODE_SNIPPET"));
                _DBM.UpdateRow(_CODE_ID, "CODE_SNIPPET", value);
            }
        }


    }
}
